package com.yanqu.road.dao.impl.activity.ghost;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostBossData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostBossDataDaoImpl extends TempDataObjectDao<GhostBossData> {

    public static String getGhostBossDataSql = "select * from t_c_activity_ghost_boss where activityId=? and batchNo=?";

    public Map<Integer, Map<Integer, GhostBossData>> getGhostBossDataMap(int activityId, int batchNo) {
        Map<Integer, Map<Integer, GhostBossData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostBossDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, batchNo);
            rs = call.executeQuery();
            while (rs.next()) {
                GhostBossData ghostBossData = parse(rs);

                if (!map.containsKey(ghostBossData.getBossType())) {
                    map.put(ghostBossData.getBossType(), new ConcurrentHashMap<>());
                }

                map.get(ghostBossData.getBossType()).put(ghostBossData.getBossId(), ghostBossData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getGhostMaxBossIdSql = "select max(bossId) as maxId from t_c_activity_ghost_boss where activityId=?";

    public Integer getGhostMaxBossId(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostMaxBossIdSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGhostBossDataSql = "update t_c_activity_ghost_boss set bossType=?,totalHp=?,curHp=?,userId=?,killer=?,killTime=?,createTime=? where activityId=? and batchNo=? and bossId=?";

    @Override
    protected boolean updateData(GhostBossData ghostBossData) {
        ghostBossData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getBossType()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getTotalHp()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getCurHp()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostBossData.getKiller())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getKillTime()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getBossId()));
        if (getDb().executeNoneQuery(updateGhostBossDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGhostBossDataSql = "insert into t_c_activity_ghost_boss(activityId,batchNo,bossId,bossType,totalHp,curHp,userId,killer,killTime,createTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GhostBossData ghostBossData) {
        ghostBossData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getBossId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostBossData.getBossType()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getTotalHp()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getCurHp()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostBossData.getKiller())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getKillTime()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostBossData.getCreateTime()));
        if (getDb().executeNoneQuery(insertGhostBossDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GhostBossData parseData(ResultSet rs) throws SQLException {
        GhostBossData ghostBossData = new GhostBossData();
        ghostBossData.setActivityId(rs.getInt("activityId"));
        ghostBossData.setBatchNo(rs.getInt("batchNo"));
        ghostBossData.setBossId(rs.getInt("bossId"));
        ghostBossData.setBossType(rs.getInt("bossType"));
        ghostBossData.setTotalHp(rs.getLong("totalHp"));
        ghostBossData.setCurHp(rs.getLong("curHp"));
        ghostBossData.setUserId(rs.getLong("userId"));
        String killer = rs.getString("killer");
        ghostBossData.setKiller(StringUtils.isNullOrEmpty(killer) ? new UserBaseInfo() : JSONObject.parseObject(killer, UserBaseInfo.class));
        ghostBossData.setKillTime(rs.getLong("killTime"));
        ghostBossData.setCreateTime(rs.getLong("createTime"));
        return ghostBossData;
    }
}
